package vn.com.pavibook.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import vn.com.pavibook.admin.service.ManageMoneyService;
import vn.com.pavibook.domain.model.PagingInfo;
import vn.com.pavibook.model.vo.MoneyVO;

@Controller
public class ManageMoneyController {
	private static final int offset = 10;
	
	private static final Logger logger = LoggerFactory.getLogger(ManageBookController.class);
	
	@Autowired
	private ManageMoneyService manageMoneyService;
	
	//info
	@RequestMapping(value = "admin/money-info")
	public String moneyInfo(Model model){
		logger.info("View list money");
		PagingInfo pagingInfo = new PagingInfo(offset, 1);
		manageMoneyService.viewListMoney(model, pagingInfo, null);
		return "admin/money-info";
	}
	
	//search
	@RequestMapping(value = "/admin/searchMoney", method = RequestMethod.POST)
	public String searchMoney(Model model, 
			@RequestParam("pageIndex") int pageIndex, @RequestParam("keyword") String keyword) {
		
		PagingInfo pagingInfo = new PagingInfo(offset, pageIndex);
		if(StringUtils.isEmpty(keyword)){
			manageMoneyService.viewListMoney(model, pagingInfo, null);
		} else {
			manageMoneyService.viewListMoney(model, pagingInfo, keyword);
		}
		return "admin/money-info";
	}
	
	//delete
	@RequestMapping(value = "/admin/delete-money", method=RequestMethod.GET)
	public String deleteBanner(Model model, @RequestParam("id") int id){
		logger.debug("controller delete banner");
		manageMoneyService.deleteMoney(id);
		return "redirect:money-info";
	}
	
	//create add new
	@RequestMapping(value = "/admin/add-money", method=RequestMethod.GET)
	public String createAddNewMoney(Model model){
		logger.debug("controller create money");
		MoneyVO moneyVO  = new MoneyVO();
		model.addAttribute("moneyVO", moneyVO);
		return "admin/add-money";
	}
	
	//edit
	@RequestMapping(value = "/admin/edit-money", method=RequestMethod.GET)
	public String editMoney(Model model, @RequestParam("id") int id){
		logger.debug("controller edit money");
		MoneyVO moneyVO  = manageMoneyService.getMoneyById(id);
		model.addAttribute("moneyVO", moneyVO);
		model.addAttribute("isEdit", true);
		return "admin/add-money";
	}
	
	//save add new
	@RequestMapping(value = "/admin/save-money")
	public String saveBook(Model model, @ModelAttribute("moneyVO") @Valid MoneyVO moneyVO, BindingResult result,HttpServletRequest request){
		logger.debug("controller saveBook");
		boolean isEdit = false;
		if(moneyVO.getId() != 0){
			isEdit = true;
		}
		
		if(result.hasErrors()){
			if(isEdit){
				model.addAttribute("isEdit", true);
			}
			return "admin/add-money";
		
		}
		
		moneyVO.setCurrency(moneyVO.getCurrency().toUpperCase());
		if(!isEdit){
		boolean checkExistCurrentcy = manageMoneyService.checkExistCurrency(moneyVO.getCurrency());
			if(checkExistCurrentcy){
				model.addAttribute("isExistCurrentcy", true);
				return "admin/add-money";
			}
		}
		manageMoneyService.saveMoney(moneyVO, isEdit);
		
		return "redirect:money-info";
	}
}
